/*
 * 文 件 名 : PlanDAO.java
 * 版    权 : China Machi.Co.,Ltd.Copyright 2009-2011.All rights reserved
 * 描    述 : <描述>
 * 修 改 人 : wangchd
 * 修改时间 : 2013-1-23
 * 跟踪单号 : <跟踪单号>
 * 修改单号 : <修改单号>
 * 修改内容 : <修改内容>
 */
package com.fire8.dolphin.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.fire8.dolphin.model.Plan;
import com.fire8.dolphin.utils.LogUtil;
import com.fire8.dolphin.utils.StringUtil;

/**
 * <一句话功能简介><br>
 * <功能详细描述>
 *
 * @author  wangchd
 * @version [版本号,2013-1-23]
 * @see     [相关类/方法]
 * @since   [产品/模块]
 */
public class PlanDAO
{
private LixgDBHelper dbHelper;
    
    public PlanDAO(Context context)
    {
        dbHelper = new LixgDBHelper(context);
    }
    
    public void addList(List<Plan> planList){
        if(planList.size()>0){
            for (Plan plan : planList)
            {
                add(plan);
            }
        }
    }
    
    public  void add(Plan plan)
    {
        if(isExist(plan.getsRemoteId()))
        {
            return;
        }
        if(StringUtil.isEmpty(plan.getTitle()))
        {
            return;
        }
        ContentValues values = new ContentValues();
       // values.put(Plan.C_LOCAL_ID, plan.getpLocalId());
        values.put(Plan.C_REMOTE_ID, plan.getsRemoteId());
        values.put(Plan.C_DATE_TIME, plan.getDateTime());
        values.put(Plan.C_ALARM_TIME, plan.getAlarmTime());
        values.put(Plan.C_TITLE, plan.getTitle());
        values.put(Plan.C_ALARM, plan.getAlarm());
        values.put(Plan.C_FINISH, plan.getFinish());
        values.put(Plan.C_IS_PUBLIC, plan.getIsPublic());
        values.put(Plan.C_USER_ID, plan.getUserId());
        long id = dbHelper.getWritableDatabase().insert(Plan.TABLE_NAME, null, values );
        plan.setpLocalId(id);
        dbHelper.close();
    }
    
    public void update(Plan plan)
    {
        ContentValues values = new ContentValues();
        values.put(Plan.C_REMOTE_ID, plan.getsRemoteId());
        values.put(Plan.C_ALARM, plan.getAlarm());
        values.put(Plan.C_FINISH, plan.getFinish());
        values.put(Plan.C_IS_PUBLIC, plan.getIsPublic());
        values.put(Plan.C_USER_ID, plan.getUserId());
        String[] args = {String.valueOf(plan.getpLocalId())};
        
        dbHelper.getWritableDatabase().update(Plan.TABLE_NAME, values, Plan.C_LOCAL_ID +" =?", args);
        dbHelper.close();
        LogUtil.d("修改计划remoteId="+plan.getsRemoteId());
        LogUtil.d("修改计划："+plan.getpLocalId());
    }
    private boolean isExist(Long remoteId)
    {
        boolean exits=false;
        if(remoteId == null || remoteId<=0)
        {
            return false;
        }
        List<String> args = new ArrayList<String>();
        StringBuffer sql = new StringBuffer("SELECT ");
        sql.append(Plan.C_REMOTE_ID)
        .append(" FROM ")
        .append(Plan.TABLE_NAME);
        sql.append(" where ").append(Plan.C_REMOTE_ID).append("=?");
        args.add(String.valueOf(remoteId));
        Cursor cursor = dbHelper.getReadableDatabase().rawQuery(sql.toString(), args.toArray(new String[]{}));
        if(cursor != null)
        {
            if(cursor.moveToNext())
            {
                exits = true;
            }
            cursor.close();
        }
        dbHelper.close();
        return exits;
    }
    
    public Plan getPlan(Long id)
    {
        List<String> args = new ArrayList<String>();
        StringBuffer sql = new StringBuffer("SELECT ");
        sql.append(Plan.C_LOCAL_ID)
        .append(",").append(Plan.C_REMOTE_ID)
        .append(",").append(Plan.C_DATE_TIME)
        .append(",").append(Plan.C_ALARM_TIME)
        .append(",").append(Plan.C_TITLE)
        .append(",").append(Plan.C_ALARM)
        .append(",").append(Plan.C_FINISH)
        .append(",").append(Plan.C_IS_PUBLIC)
        .append(",").append(Plan.C_USER_ID)
        .append(" FROM ")
        .append(Plan.TABLE_NAME)
        .append(" where 1=1 ");
        if(id != null && id>0)
        {
            sql.append(" and ").append(Plan.C_LOCAL_ID).append("=?");
            args.add(String.valueOf(id));
        }
        Cursor cursor = dbHelper.getReadableDatabase().rawQuery(sql.toString(), args.toArray(new String[]{}));
        List<Plan> list = new ArrayList<Plan>();
        cursor2Object(cursor, list);
        if(cursor != null)
        {
            cursor.close();
            cursor = null;
        }
        dbHelper.close();
        if(!list.isEmpty())
        {
            return list.get(0);
        }
        return null;
    }
    
    public HashMap<String, String> getObject(Long id)
    {
        List<Long> args = new ArrayList<Long>();
        StringBuffer sql = new StringBuffer("SELECT ");
        sql.append(Plan.C_LOCAL_ID)
        .append(",").append(Plan.C_REMOTE_ID)
        .append(",").append(Plan.C_DATE_TIME)
        .append(",").append(Plan.C_ALARM_TIME)
        .append(",").append(Plan.C_TITLE)
        .append(",").append(Plan.C_ALARM)
        .append(",").append(Plan.C_FINISH)
        .append(",").append(Plan.C_IS_PUBLIC)
        .append(",").append(Plan.C_USER_ID)
        .append(" FROM ")
        .append(Plan.TABLE_NAME)
        .append(" where 1=1 ");
        if(!id.equals(0l) && id!=null)
        {
            sql.append(" and ").append(Plan.C_LOCAL_ID).append("=?");
            args.add(id);
        }
        Cursor cursor = dbHelper.getReadableDatabase().rawQuery(sql.toString(), args.toArray(new String[]{}));
        List<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
        dbHelper.cursorToHashMap(cursor, list);
        if(cursor != null)
        {
            cursor.close();
            cursor = null;
        }
        dbHelper.close();
        if(!list.isEmpty())
        {
            return list.get(0);
        }
        return null;
    }
    
    public List<Plan> findList(Plan plan)
    {
        List<Long> args = new ArrayList<Long>();
        StringBuffer sql = new StringBuffer("SELECT ");
        sql.append(Plan.C_LOCAL_ID)
        .append(",").append(Plan.C_REMOTE_ID)
        .append(",").append(Plan.C_DATE_TIME)
        .append(",").append(Plan.C_ALARM_TIME)
        .append(",").append(Plan.C_TITLE)
        .append(",").append(Plan.C_ALARM)
        .append(",").append(Plan.C_FINISH)
        .append(",").append(Plan.C_IS_PUBLIC)
        .append(",").append(Plan.C_USER_ID)
        .append(" FROM ")
        .append(Plan.TABLE_NAME)
        .append(" where 1=1 ");
        if(plan != null )
        {
            sql.append(" and ").append(plan.getpLocalId()).append("=?");
            args.add(plan.getpLocalId());
        }
        
        Cursor cursor = dbHelper.getReadableDatabase().rawQuery(sql.toString(), args.toArray(new String[]{}));
        List<Plan> list = new ArrayList<Plan>();
        cursor2Object(cursor,list);
        if(cursor != null)
        {
            cursor.close();
            cursor = null;
        }
        dbHelper.close();
        return list;
    }
    public void delete()
    {
        dbHelper.getWritableDatabase().delete(Plan.TABLE_NAME, null, null);
    }
    public void deletePlan(Long id)
    {
        String[] args = {String.valueOf(id)}; 
        dbHelper.getWritableDatabase().delete(Plan.TABLE_NAME, Plan.C_LOCAL_ID+"=?", args);
        dbHelper.close();
        LogUtil.d("删除计划："+id);
    }
    private void cursor2Object(Cursor cursor, List<Plan> list)
    {
        if(cursor != null)
        {
            while(cursor.moveToNext())
            {
                
                Plan plan = new Plan();
                plan.setpLocalId(cursor.getLong(cursor.getColumnIndex(Plan.C_LOCAL_ID)));
                plan.setDateTime(cursor.getString(cursor.getColumnIndex(Plan.C_DATE_TIME)));
                plan.setAlarmTime(cursor.getString(cursor.getColumnIndex(Plan.C_ALARM_TIME)));
                plan.setsRemoteId(cursor.getLong(cursor.getColumnIndex(Plan.C_REMOTE_ID)));
                plan.setTitle(cursor.getString(cursor.getColumnIndex(Plan.C_TITLE)));
                plan.setAlarm(cursor.getInt(cursor.getColumnIndex(Plan.C_ALARM)));
                plan.setFinish(cursor.getInt(cursor.getColumnIndex(Plan.C_FINISH)));
                plan.setIsPublic(cursor.getInt(cursor.getColumnIndex(Plan.C_IS_PUBLIC)));
                plan.setUserId(cursor.getInt(cursor.getColumnIndex(Plan.C_USER_ID)));
                
                LogUtil.d("是否完成"+plan.getFinish());
                //LogUtil.d("提醒时间"+plan.getAlarmTime());
                list.add(plan);
            }
            cursor.close();
            cursor = null;
        }
    }

}
